<?php
namespace plugins\finance\model;

use plugins\pay\model\PayLogModel;
use think\Model;
use think\Db;
use plugins\finance\model\FinanceModel;

class FinanceAccountsModel extends Model
{

    //创建一条流水
    private function createAccounts($arr)
    {
        // Db::startTrans();
        try{
            foreach ($arr as $key => &$value) {
                // finance_id	财务账户id
                // finance_type
                // finance_type_id
                $check = ( new FinanceModel )
                    ->get(['finance_type'=>$value['finance_type'],'finance_id'=>$value['finance_id']]);
                if(!$check){
                    return ['status'=>false,'message'=>'不存在的财务账户'];
                }
                //账户id
                $value['finance_id'] = $check->finance_id;
                $value['account_num'] = uniqid('account_');

                //显示字符串拼接
                $value['money_string'] = $value['money_type'].$value['money'];
            }
            $result = $this->allowField(true)->saveAll($arr);
            if(!$result){
                return ['status'=>false,'message'=>$check->finance_type.'添加失败'];
            }

            // Db::commit();
            return ['status'=>true,'message'=>'添加成功'];
        }catch(\Exception $e){
            // Db::rollback();
            // db('test')->insert(['content'=>$e->getMessage(),'time'=>date('Y-m H:i:s',time()),'remark'=>'执行错误,内']);
            return ['status'=>false,'message'=>$e->getMessage()];
        }

    }

    //行为的检测
    //$num 行为编号
    //$data 传参
    // use plugins\finance\model\FinanceAccountsModel;
    // ( new FinanceAccountsModel )->behavior($num,$data['remark','money','store','member']);
    // $data['remark'] = '订单支付';
    // $data['money'] = $order->order_money;
    // $data['member'] = $member_finance->finance_id;
    // $data['store'] =  $store_finance->finance_id;
    // ( new FinanceAccountsModel )->behavior(201,$data);
    public function behavior($num,$data=null)
    {

        // PayLogModel::W( 4 , $num , $data );
        // Db::startTrans();
        try{

            if($data != null){
                //共通数据
                $arr = [];
                //备注
                $arr['remark'] = $data['remark'];
                //金额
                $arr['money'] = $data['money'];
                //时间
                $arr['time'] = time();
                //行为编号
                $arr['behavior'] = $num;
            }


            switch ($num) {
                //店铺充值 平台加 店铺加
                case '101':
                    if($data == null){
                        return '店铺充值';
                    }

                    //行为共通参数
                    $arr['account_type'] = 'balance';
                    //店铺加
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '+';
                    $list['store'] += $arr;

                    //平台加
                    $list['admin']['finance_type'] = 'admin';
                    $list['admin']['finance_id'] = 'admin';
                    $list['admin']['money_type'] = '+';
                    $list['admin'] += $arr;

                    break;
                //店铺提现 平台减 店铺减
                case '102':
                    if($data == null){
                        return '店铺提现';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'balance';

                    //店铺加
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '-';
                    $list['store'] += $arr;

                    //平台加
                    $list['admin']['finance_type'] = 'admin';
                    $list['admin']['finance_id'] = 'admin';
                    $list['admin']['money_type'] = '-';
                    $list['admin'] += $arr;

                    break;
                //用户充值 平台加 用户加
                case '103':
                    if($data == null){
                        return '用户充值';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'balance';

                    //用户加
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '+';
                    $list['member'] += $arr;

                    //平台加
                    $list['admin']['finance_type'] = 'admin';
                    $list['admin']['finance_id'] = 'admin';
                    $list['admin']['money_type'] = '+';
                    $list['admin'] += $arr;

                    break;
                //用户提现 平台减 用户减
                case '104':
                    if($data == null){
                        return '用户提现';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'balance';

                    //用户减
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '-';
                    $list['member'] += $arr;

                    //平台减
                    $list['admin']['finance_type'] = 'admin';
                    $list['admin']['finance_id'] = 'admin';
                    $list['admin']['money_type'] = '-';
                    $list['admin'] += $arr;
                    break;
                //用户充值退款  平台减  用户减
                case '105':
                    if($data == null){
                        return '用户充值退款';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'balance';

                    //用户加
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '-';
                    $list['member'] += $arr;

                    //平台加
                    $list['admin']['finance_type'] = 'admin';
                    $list['admin']['finance_id'] = 'admin';
                    $list['admin']['money_type'] = '-';
                    $list['admin'] += $arr;

                    break;
                //订单外部接口支付 平台加 店铺预增加 用户减
                case '201':

                    if($data == null){
                        return '订单支付';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'order';

                    //店铺预增加
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '+';
                    $list['store'] += $arr;

                    //平台加
//                    $list['admin']['finance_type'] = 'store';
//                    $list['admin']['finance_id'] = 'admin';
//                    $list['admin']['money_type'] = '+';
//                    $list['admin'] += $arr;

                    //用户减
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '-';
                    $list['member'] += $arr;


                    break;
                //订单外部支付确认收货 平台减 店铺入账
                case '202':
                    if($data == null){
                        return '订单确认收货';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'order';

                    //平台减
                    $list['admin']['finance_type'] = 'admin';
                    $list['admin']['finance_id'] = 'admin';
                    $list['admin']['money_type'] = '-';
                    $list['admin'] += $arr;

                    //店铺入账
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '入账';
                    $list['store'] += $arr;

                    break;
                //未完成外部订单退款 平台减 店铺未入账取消 用户加
                case '203':
                    if($data == null){
                        return '未完成订单退款';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'order';

                    //平台减
                    $list['admin']['finance_type'] = 'admin';
                    $list['admin']['finance_id'] = 'admin';
                    $list['admin']['money_type'] = '-';
                    $list['admin'] += $arr;

                    //店铺预增加取消
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '退款';
                    $list['store'] += $arr;

                    //用户加
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '+';
                    $list['member'] += $arr;

                    break;
                //完结外部订单退款 店铺减 用户加 平台-
                case '204':
                    if($data == null){
                        return '完结订单退款';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'order';

                    //店铺减
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '-';
                    $list['store'] += $arr;

                    //用户加
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '+';
                    $list['member'] += $arr;

                    //平台减
                    $list['admin']['finance_type'] = 'admin';
                    $list['admin']['finance_id'] = 'admin';
                    $list['admin']['money_type'] = '-';
                    $list['member'] += $arr;

                    break;
                //订单余额接口支付 店铺预增加 用户减
                case '301':
                    if($data == null){
                        return '订单支付';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'order';

                    //店铺预增加
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '未入账';
                    $list['store'] += $arr;

                    //用户减
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '-';
                    $list['member'] += $arr;

                    break;
                //订单余额支付确认收货 店铺入账
                case '302':
                    if($data == null){
                        return '订单确认收货';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'order';

                    //店铺入账
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '入账';
                    $list['store'] += $arr;

                    break;
                //未完成余额订单退款 店铺未入账取消 用户加
                case '303':
                    if($data == null){
                        return '未完成订单退款';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'order';

                    //店铺预增加取消
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '退款';
                    $list['store'] += $arr;

                    //用户加
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '+';
                    $list['member'] += $arr;

                    break;
                //完结余额订单退款 店铺减 用户加
                case '304':
                    if($data == null){
                        return '完结订单退款';
                    }
                    //行为共通参数
                    $arr['account_type'] = 'order';

                    //店铺减
                    $list['store']['finance_type'] = 'store';
                    $list['store']['finance_id'] = $data['store'];
                    $list['store']['money_type'] = '-';
                    $list['store'] += $arr;

                    //用户加
                    $list['member']['finance_type'] = 'member';
                    $list['member']['finance_id'] = $data['member'];
                    $list['member']['money_type'] = '+';
                    $list['member'] += $arr;

                    break;

                default:
                    if($data == null){
                        return '';
                    }
                    return ['status'=>false,'messgae'=>'没有指定的代码'];
                    break;
            }

            //递交
            $result = $this->createAccounts($list);

            if($result['status'] == true){
                // Db::commit();
            }

            return $result;

        }catch(\Exception $e){
            // Db::rollback();
            db('test')->insert(['content'=>$e->getMessage(),'time'=>date('Y-m H:i:s',time()),'remark'=>'执行错误,外']);
            // return ['status'=>false,'message'=>$e->getMessage()];
        }
    }

}